package com.rerun.common;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * The type Http helper.
 */
public class HttpHelper {

    /**
     * 获取 HttpServletRequest
     *
     * @return the http servlet request
     */
    public static HttpServletRequest getHttpServletRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

    /**
     * Url string.
     *
     * @param request the request
     * @return the string
     */
    public static String  url(HttpServletRequest request){
      StringBuilder stringBuilder=new StringBuilder("http://");
       String ip=  request.getRemoteAddr();
       int port=request.getRemotePort();
      return stringBuilder.append(ip).append(port).toString();
    }

    /**
     * Gets base path.
     *
     * @param request the request
     * @return the base path
     */
    public static String getBasePath(HttpServletRequest request) {
        String port = String.valueOf(request.getServerPort());
        if(port.equals("443")||port.equals("80")) port = "";
        else port = ":" + port;
        String path = request.getContextPath();
        return request.getScheme()+"://"+request.getServerName()+port+path;
    }

}
